Skip to content

perf(buy-modal): derive oracle key with useMemo instead of effect + state (#543) - #632

Merged
nonsobethel0-dev merged 2 commits into
Parashield-Protocol:mainfrom
presidojay1:fix/543-derive-oracle-key-usememo
Sep 23, 2026
Merged

nonsobethel0-dev merged 2 commits into
Parashield-Protocol:mainfrom
presidojay1:fix/543-derive-oracle-key-usememo

Conversation

@presidojay1

@presidojay1 presidojay1 commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

BuyPolicyModal copied the oracle key into state from a useEffect:

useEffect(() => {
  if (product.category === 'crop') setOracleKey(buildRainfallKey(...));
  else if (product.category === 'flight') setOracleKey(buildFlightKey(...));
  else if (product.category === 'defi') setOracleKey('defi');
}, [product.category, lat, lng, year, month, flightNumber, flightDate]);

This caused two problems:

  • Every keystroke in the latitude, longitude, year, month, flight number or date fields rendered the whole modal twice: once for the input change and again when the effect called setOracleKey.
  • The first of those renders showed the previous key.

Fix

This follows the approach suggested in the issue:

  • oracleKey is now computed during render with useMemo, from product.category and the builder inputs, so there's no effect and no extra render.
  • Categories without a key builder (disaster, health, …) still need the free-text key input. That input now uses a separate state, manualOracleKey, and useMemo returns it for those categories.
  • Everything downstream is unchanged: validation (32-char limit, required key), the "Computed Key" preview, the confirm step and invokeBuyPolicy / buyPolicy all still read oracleKey.

Tests

Added in src/__tests__/BuyPolicyModal.test.tsx:

  • Typing a crop latitude updates the computed key right away (rainfall:1.5…)
  • Typing a flight number updates the computed key (flight:KQ100:…)
  • Manual key entry still works for a health product
npx vitest run src/__tests__/BuyPolicyModal.test.tsx
 Tests  7 failed | 6 passed (13)   # all 3 new tests pass

The 7 failures already happen on main, before this change. All of them are the existing renderToStaticMarkup tests. Modal renders nothing on the server, so the markup comes back as ''. purchase-flow.integration.test.tsx > shows validation error for empty coverage also fails on main. With this PR the set of failing tests is exactly the same as on main. Those tests are out of scope here and should be fixed separately.

tsc --noEmit reports no new errors in BuyPolicyModal.tsx. The existing makeProduct type error in the test file was already there.
Closes #543
Closes #540
Closes #541
Closes #542

BuyPolicyModal synced the oracle key into state from a useEffect, so
every keystroke in the lat/lng/year/month/flight fields rendered twice
(input change, then setOracleKey) and the first render showed a stale key.

Compute the key during render with useMemo from the builder inputs.
Only categories without a key builder (disaster, health, ...) keep
state, now named manualOracleKey, for the free-text input.

Closes Parashield-Protocol#543
Assert the computed key updates as crop latitude and flight number are
typed, and that manual oracle-key entry still works for categories
without a builder.

Refs Parashield-Protocol#543
@drips-wave

drips-wave Bot commented Sep 23, 2026

Copy link
Copy Markdown

@presidojay1 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

@netlify

netlify Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

❌ Deploy Preview for boisterous-sunshine-dd4c4c failed.

Name Link
🔨 Latest commit 0cebcce
🔍 Latest deploy log https://app.netlify.com/projects/boisterous-sunshine-dd4c4c/deploys/6ab427379744c200085c4b04

@nonsobethel0-dev
nonsobethel0-dev merged commit e9959a1 into Parashield-Protocol:main Sep 23, 2026
0 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants